﻿// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");

using System;
using System.Numerics;

namespace pro_3
{
    public class Test
    {
        public static void Main()
        {
            System.Console.WriteLine("Random figures : ");
            Random r = new Random();
            PlaneFigure[] pics=new PlaneFigure[10];
            double areaSum = 0;
            for(int i = 0; i < 10; i++)
            {
                int type = r.Next(3);
                switch (type)
                {
                    case 0:
                        double rds = r.NextDouble()*100;
                        pics[i] = new Circle(rds);
                        break;
                    case 1:
                        double l = r.NextDouble()*100;
                        pics[i] = new Square(l);
                        break;
                    case 2:
                        double x = r.NextDouble() * 100;
                        double y = r.NextDouble() * 100;
                        pics[i] = new Rectangle(x, y);
                        break;
                }
                System.Console.Write("" + (i+1) + " : ");
                pics[i].printInfo();
                areaSum += pics[i].getArea();
            }
            System.Console.WriteLine("Sum area : " + areaSum);
        }
    }

    /*
     *平面图形接口，
     *包括确认图形是否合法、返回面积、打印相关消息三个方法
     */
    interface PlaneFigure
    {
        public bool isEffective();
        public double getArea();
        public void printInfo();
    }

    /*
     *圆类
     *包括属性radius及其索引Radius
     *实现了接口的三个方法
     */
    public class Circle: PlaneFigure {
        private double radius;
        public Circle() {
            radius = 0;
        }

        public Circle(double length) {
            radius = length;
        }

        public double Radius
        {
            set { radius = value; }
            get { return radius; }
        }

        public bool isEffective() {
            return radius >= 0;
        }

        public double getArea() {
            if (!isEffective())
                return 0;
            return double.Pi * radius * radius;
        }

        public void printInfo()
        {
            if (isEffective())
                System.Console.WriteLine("Circle: Radius = " + radius + " , Area = " + getArea());
            else 
                System.Console.WriteLine("Circle: Radius <= 0 , illegal.");
        }
    }

    /*
     *矩形类
     *包括属性length、height及其索引Length、Height
     *实现了接口的三个方法
     */
    public class Rectangle : PlaneFigure
    {
        private double length;
        private double height;

        public double Length
        {
            set { length = value; }
            get { return length; }
        }
        public double Height
        {
            set { height = value; }
            get { return height; }
        }

        public Rectangle() {
            length = 0;
            height = 0;
        }
        public Rectangle(double x) {
            length = x;
            height = 0;
        }
        public Rectangle(double x, double y)
        {
            length = x;
            height = y;
        }

        public bool isEffective() { return length >= 0 && height >= 0; }
        public double getArea()
        {
            if (!isEffective())
                return 0; 
            return length * height; 
        }
        public void printInfo()
        {
            if (isEffective())
                System.Console.WriteLine("Rectangle: Length = " + length + 
                    " , Height = " + height+" , Area = "+getArea());
            else
                System.Console.WriteLine("Rectangle: illegal.");
        }
    }


    /*
     *正方形类
     *包括属性length及其索引Length
     *实现了接口的三个方法
     */
    public class Square : PlaneFigure {
        private double length; 
        
        public double Length
        {
            set { length = value; }
            get { return length; }
        }

        public Square()
        {
            length = 0;
        }
        public Square(double x)
        {
            length = x;
        }

        public double getArea()
        {
            if (!isEffective())
                return 0; 
            return length * length; 
        }
        public bool isEffective() { return length > 0; }
        public void printInfo()
        {
            if (isEffective())
                System.Console.WriteLine("Square: Length = " + length + " , Area = " + getArea());
            else
                System.Console.WriteLine("Square: illegal.");
        }
    }
}
